package ai4games_tests.guides;

import game2dai.World;
import game2dai.entities.Vehicle;
import game2dai.entityshapes.ps.BitmapPic;
import game2dai.entityshapes.ps.Hints;
import game2dai.maths.Vector2D;
import game2dai.utils.StopWatch;
import processing.core.PApplet;

public class Tank_01 extends PApplet {
	// Global variable declaration area
	World world;
	StopWatch sw;
	Vehicle tank;
	Vector2D target = new Vector2D();
	BitmapPic view;
	
	public void setup(){
		size(600,320);
		world = new World(width, height);
		sw = new StopWatch();
		// Create the mover
		tank = new Vehicle(new Vector2D(width/2,height/2), // position
				40, // collision radius
				new Vector2D(0,0), // velocity
				40, // maximum speed
				new Vector2D(1,0), // heading
				15, // mass
				1.5,  // turning rate
				1000 // max force
		); 
		// What does this mover look like
		view = new BitmapPic(this, "ai4g/tanks.png", 8, 1);		
		view.showHints(Hints.HINT_COLLISION | Hints.HINT_HEADING | Hints.HINT_VELOCITY);
		tank.renderer(view);
		// Finally we want to add this to our game domain
		world.add(tank);
		
		tank.forceRecorderOn();
		sw.reset();
	}
	
	public void draw(){
		double elapsedTime = sw.getElapsedTime();
		target.set(mouseX, mouseY);
		tank.AP().arriveOn(target);
		float speed = (float) tank.speed();
		float maxSpeed = (float) tank.maxSpeed();		
		if(speed > 1){
			float newInterval = map(speed, 0, maxSpeed, 0.6f, 0.04f);
			view.setAnimation(newInterval, 1);
			System.out.println("Speed " + speed + "   " + newInterval);
		}
		else {
			view.pauseAnimation();
		}
		world.update(elapsedTime);
		background(218,140,54);
		world.draw(elapsedTime);
	}
	
	public void keyTyped(){
		if(key == 'f')
			world.printForceData();
	}
}
